import { Component, OnInit } from "@angular/core";
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router } from "@angular/router"; //路由引入

declare var $: any;
@Component({
  selector: "administrative-divisions",
  templateUrl: "./administrative-divisions.component.html",
  styleUrls: ["./administrative-divisions.css"],
})
export class AdministrativeDivisionsComponent implements OnInit {
  // initData: any = {
  //   id: '',
  //   areaName: '',
  //   level: 2,
  // }
  addRegion: any = {
    level: 2,
    areaName: '',
    areaNumber: '',
    parentId: ''
  }
  cityId: string = '';
  provinceId: string = '';
  levelOption: any = [
    {
      text: '[一级]省级行政区',
      value: 2
    },{
      text: '[二级]地级行政区',
      value: 3
    },{
      text: '[三级]县级行政区',
      value: 4
    }
  ]
  provinceList: any = [];
  cityList: any = [];
  countyList: any = [];
  provinceSelected: string = ''; // 一级表格选中的id
  citySelected: string = ''; // 二级表格选中的id
  countySelected: string = ''; // 三级表格选中的id
  provinceKey: string = ''; // 输入的一级搜索
  cityKey: string = ''; // 输入的二级搜索
  countyKey: string = ''; // 输入的三级搜索
  twoAreaName: string = '';
  threeAreaName: string = '';
  editState: boolean = false; // 编辑状态
  editId: string = '';
  constructor(private jsonApiService: JsonApiService, private router: Router) {}
  ngOnInit() {
    this.load(2);
  }
  //table刷新
  refresh() {
    this.load(2);
  }
  //初始化加载list数据  TODO
  load(level, id?) {
    let data = {
      level: level,
      id: id
    }
    this.jsonApiService.http_obj("region/list.html", data).subscribe((result) => {
      if(level == 3) {
        this.cityList = result.data || [];
        this.citySelected = '';
        this.countyList = [];
      }else if(level == 4) {
        this.countyList = result.data || [];
        this.countySelected = '';
      }else {
        this.provinceList = result.data || [];
        this.cityList = [];
        this.countyList = [];
        this.provinceSelected = '';
        this.citySelected = '';
        this.countySelected = '';
      }
    });
  }
  // 添加
  addShowModal(modal) {
    this.editState = false;
    for(let key in this.addRegion){
      this.addRegion[key] = '';
    }
    this.addRegion.level = 2;
    this.provinceId = '';
    this.cityId = '';
    modal.show();
  }
  // 选择层级
  changeLevel(val) {}
  // 选择行政区划一级
  changeProvinceOption(val) {
    if(val) {
      this.cityId = '';
      this.load(3, val);
    }
  }
  changeCityOption(val) {
    if(val) {
      this.load(4, val);
    }
  }
  addRegionClose(modal) {
    modal.hide();
  }
  // 提交添加
  addRegionSubmit(modal) {
    if(this.addRegion.level == 3){
      if(this.provinceId){
        this.addRegion.parentId = this.provinceId;
      }else {
        this.jsonApiService.message('请选择上级单位[一级]');
        return;
      }
    }else if(this.addRegion.level == 4){
      if(this.cityId){
        this.addRegion.parentId = this.cityId;
      }else {
        this.jsonApiService.message(this.provinceId?'请选择上级单位[二级]':'请选择上级单位[一级]');
        return;
      }
    }
    // if(!this.addRegion.areaNumber){
    //   this.jsonApiService.message('请填写行政区划代码');
    //   return;
    // }
    if(!this.addRegion.areaName){
      this.jsonApiService.message('请填写单位名称');
      return;
    }
    this.addRegion.areaNumber = this.addRegion.areaNumber?this.addRegion.areaNumber:'';
    if(this.editState) {
      this.addRegion.id = this.editId;
    }
    this.jsonApiService.http_obj("region/modify.html", this.addRegion).subscribe((result) => {
      this.jsonApiService.message(result.message);
      if(this.addRegion.level == 2) {
        this.load(this.addRegion.level);
      }else if(this.addRegion.level == 3){
        this.load(this.addRegion.level, this.provinceId);
      }else {
        this.load(this.addRegion.level, this.cityId);
      }
      modal.hide();
    });
  }
  // 删除
  regionDelete(level) {
    let curId = '';
    if(level == 2) {
      curId = this.provinceSelected;
    }else if(level == 3) {
      curId = this.citySelected;
    }else {
      curId = this.countySelected;
    }
    let text = level == 4?'此操作将永久删除数据,确认要删除?':'此操作将永久删除数据,确认要删除?<br/><span style="color: #D9001B;">（此单位包含了其他下级单位，删除时会一并删除）</span>'
    this.jsonApiService.confirm_message(text).then(()=>{
			this.jsonApiService.http_obj("region/delete.html", {id:curId})
      .subscribe((result) => {
        if(level == 2) {
          this.load(level);
        }else if(level == 3){
          this.load(level, this.provinceSelected);
        }else {
          this.load(level, this.citySelected);
        }
        this.jsonApiService.message(result.message);
      });
		})
  }
  // 编辑
  regionEdit(id, modal) {
    this.editState = true;
    this.editId = id;
    this.jsonApiService.http_obj("region/listById", {id: id}).subscribe((result) => {
      if(result.data && result.data.length) {
        let editArr = result.data.filter(item => item.id == id);
        this.addRegion.level = editArr[0].level;
        this.addRegion.areaName = editArr[0].areaName;
        this.addRegion.areaNumber = editArr[0].areaNumber;
        if(editArr[0].level == 3) {
          let editParent = result.data.filter(item => item.id == editArr[0].parentId);
          this.twoAreaName = editParent[0].areaName;
          this.provinceId = editParent[0].id;
        }else if(editArr[0].level == 4) {
          let editParent = result.data.filter(item => item.id == editArr[0].parentId);
          let editParentLord = result.data.filter(item => item.id == editParent[0].parentId);
          this.twoAreaName = editParentLord[0].areaName;
          this.threeAreaName = editParent[0].areaName;
          this.cityId = editParent[0].id;
          this.provinceId = editParentLord[0].id;
        }
        modal.show();
      }
    });
  }
  // 选中一级行政区
  rowSelectEvent(row, level) {
    if(level == 3) {
      this.provinceSelected = row.id;
      this.citySelected = '';
      this.countySelected = '';
      this.countyList = [];
      this.load(level, row.id);
    }else if(level == 4){
      this.citySelected = row.id;
      this.countySelected = '';
      this.load(level, row.id);
    }else {
      this.countySelected = row.id;
    }
  }
  // 搜索
  tableSearch(level) {
    let areaName = '';
    if(level == 2) {
      areaName = this.provinceKey;
    }else if(level == 3) {
      areaName = this.cityKey;
    }else {
      areaName = this.countyKey;
    }
    if(!areaName.length) {
      if(level == 3) {
        this.cityList = [];
        this.citySelected = '';
        this.countyList = [];
        return;
      }else if(level == 4){
        this.countyList = [];
        this.countySelected = '';
        return;
      }
    }
    let data = {
      level: level,
      areaName: areaName
    }
    this.jsonApiService.http_obj("region/list.html", data).subscribe((result) => {
      if(level == 2) {
        this.provinceList = result.data || [];
        this.cityList = [];
        this.countyList = [];
        this.provinceSelected = '';
        this.citySelected = '';
        this.countySelected = '';
      }else if(level == 3) {
        this.cityList = result.data || [];
        this.citySelected = '';
        this.countyList = [];
      }else {
        this.countyList = result.data || [];
        this.countySelected = '';
      }
    });
  }
  keyupAreaNumber(e) {
    const invalidChars = ['-', '+', 'e', '.', 'E']
    if(invalidChars.indexOf(e.key) !== -1){
      e.preventDefault();
    }
  }
}
